package ar.edu.itba.it.bridge

object BridgeApp extends App {

  object Person extends Enumeration {
    type Person = Value
    val Alice, Bob, Candance, Dave = Value
    def all = List(Alice, Bob, Candance, Dave)
  }

  import Person._
  import scala.Math._

  val times = Map(Alice -> 5, Bob -> 10, Candance -> 20, Dave -> 25)

  class State(left: List[Person], lightOnLeft: Boolean, time: Int, val steps: List[Either[Person, (Person, Person)]]) {
    def next: List[State] = {
      def chooseTwo: List[(Person, Person)] = for (p1 <- left; p2 <- left - p1; if (p1 < p2 && time - max(times(p1), times(p2)) >= 0)) yield (p1, p2)

      if (lightOnLeft) {
        chooseTwo map { p =>
          val people = List(p._1, p._2)
          val newTime = time - max(times(p._1), times(p._2))
          new State(left -- people, !lightOnLeft, newTime, Right(p) :: steps)
        }
      } else all -- left map { p: Person => new State(p :: left, true, time - times(p), Left(p) :: steps) }
    }

    def isGoal = left isEmpty
  }

  val initialState = new State(all, true, 60, Nil)
  def process(state: State): Unit = if (state.isGoal) println(state.steps.reverse) else state.next map { process }

  process(initialState)
}